package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.DepartDTO;

public class DepartDao {
	public ResultSet deplist(Connection con) throws Exception{
		String sql="select * from unitinfo";
		PreparedStatement pstmt=con.prepareStatement(sql);
		return pstmt.executeQuery();
	}
    public int addDep(Connection con,DepartDTO depart) throws Exception{
	String sql="insert into unitinfo values (null,?,?,?,?,?,?)";
	PreparedStatement pstmt=con.prepareStatement(sql);
	pstmt.setString(1, depart.getUnit_id());
	pstmt.setString(2, depart.getUp_unit_id());
	pstmt.setString(3, depart.getUnit_name());
	pstmt.setString(4, depart.getUnit_abbr());
	pstmt.setString(5, depart.getTelephone());
	pstmt.setString(6, depart.getUnit_desc());
	return pstmt.executeUpdate();
}
    public int deleteDep(Connection con,String unitid) throws Exception{
    	String sql="delete from unitinfo where UNIT_ID=?";
    	PreparedStatement pstmt=con.prepareStatement(sql);
    	pstmt.setString(1, unitid);
    	return pstmt.executeUpdate();
    }
    public int modifyDep(Connection con, DepartDTO depart) throws Exception{
    	String sql="update unitinfo set UNIT_NAME=?,UNIT_ABBR=?,"
    			+ "TELEPHONE=?,UNIT_DESC=? WHERE UNIT_ID=?";
    	PreparedStatement pstmt=con.prepareStatement(sql);
    	pstmt.setString(1, depart.getUnit_name());
    	pstmt.setString(2, depart.getUnit_abbr());
    	pstmt.setString(3, depart.getTelephone());
    	pstmt.setString(4, depart.getUnit_desc());
    	pstmt.setString(5, depart.getUnit_id());
    	return pstmt.executeUpdate();
    }
}
